package com.chen.palmar.project.init;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.chen.palmar.R;
import com.chen.palmar.base.BaseActivity;
import com.chen.palmar.common.datasource.DataCenter;
import com.chen.palmar.common.override.HttpSubscriber;
import com.chen.palmar.entity.LoginEntity;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Subscription;

/**
 * Created by chen on 2017/12/28.
 */

public class IdentityActivity extends BaseActivity {

    @Bind(R.id.rg_state)
    RadioGroup rgState;
    private static int IDENTITY = 1;
    @Bind(R.id.btn_identity)
    Button btnIdentity;
    @Bind(R.id.rb_produce)
    RadioButton rbProduce;
    @Bind(R.id.rb_agency)
    RadioButton rbAgency;
    Drawable agency;
    Drawable agencyed;
    Drawable producer;
    Drawable producered;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        super.onCreate(savedInstanceState);
        init();
    }

    @Override
    public void init() {
        agency = ContextCompat.getDrawable(this, R.mipmap.id_agency_icon);
        agencyed = ContextCompat.getDrawable(this, R.mipmap.id_agencyed_icon);
        producer = ContextCompat.getDrawable(this, R.mipmap.id_producer_icon);
        producered = ContextCompat.getDrawable(this, R.mipmap.id_producered_icon);

        btnIdentity.setEnabled(false);
        rbProduce.setCompoundDrawablesRelativeWithIntrinsicBounds(null, producered, null, null);
        rgState.setOnCheckedChangeListener((group, checkedId) -> {
            btnIdentity.setEnabled(true);
            switch (checkedId) {
                case R.id.rb_produce:
                    IDENTITY = 1;
                    rbProduce.setCompoundDrawablesRelativeWithIntrinsicBounds(null, producered, null, null);
                    rbAgency.setCompoundDrawablesRelativeWithIntrinsicBounds(null, agency, null, null);
                    break;
                case R.id.rb_agency:
                    IDENTITY = 2;
                    rbProduce.setCompoundDrawablesRelativeWithIntrinsicBounds(null, producer, null, null);
                    rbAgency.setCompoundDrawablesRelativeWithIntrinsicBounds(null, agencyed, null, null);
                    break;
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_identity;
    }

    @OnClick({R.id.btn_identity})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_identity:
                commitInfo();
                break;
        }
    }

    private void commitInfo() {
        HashMap<String, String> map = new HashMap<>();
        map.put("type", IDENTITY + "");
        Subscription sub = DataCenter.firstSetting(map).subscribe(new HttpSubscriber<LoginEntity>(this, showLoading("加载中")) {
            @Override
            public void onNext(LoginEntity loginEntity) {
                DataCenter.httpConfig.setUserId(loginEntity.getData().getUserId());
                Intent intent = new Intent(IdentityActivity.this, MainActivity.class);
                intent.putExtra("info", loginEntity.getData());
                startActivity(intent);
                IdentityActivity.this.finish();
            }
        });
        subscription.add(sub);
    }
}
